package com.epalmpay.commom;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.system.RoleOutDTO;
import com.epalmpay.dto.system.UserInDTO;
import com.epalmpay.dto.system.UserListOutDTO;
import com.epalmpay.dto.system.UserOutDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.Userbase;
import com.epalmpay.enumdef.BizEnum.UserStatus;
import com.epalmpay.service.management.IButtonService;
import com.epalmpay.service.management.IMenuService;
import com.epalmpay.service.management.IRoleService;
import com.epalmpay.service.management.IUserService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;

@Controller
@RequestMapping(value="/manager/system")
public class UserController extends BaseController {
     
	@Resource  
    private IRoleService roleService;  
	@Resource  
    private IUserService userService;  
	@Resource  
    private IMenuService menuService;  
	@Resource  
    private IButtonService buttonService; 
		
	/**
	 * 显示用户分页列表
	 * 
	 */
	@RequestMapping(value="/listUsers")
	@ResponseBody
	public UserListOutDTO  listUser(UserInDTO userOut,PageDTO pageDTO){
		UserListOutDTO userOutDTO=new UserListOutDTO();
		ArrayList<UserOutDTO> userOutList=new ArrayList<UserOutDTO>();
		try {  
			Userbase user=new Userbase();
			Page page=Tools.pageEmtity(pageDTO);
			user.setPage(page); 
			user.setUsername(userOut.getUsername());//检索用户名
			user.setGroupId(userOut.getGroupId());
			user.setRoleId(userOut.getRoleId());
			user.setStartDate(userOut.getStartDate());
			user.setEndDate(userOut.getEndDate());

			List<Userbase> list=null;
			if(userOut.getGroupId()==0){
				 list=userService.listZfUsers(user);
			}else{
				 list=userService.listUsers(user);
			}
			for (int i = 0; i < list.size(); i++) {
				UserOutDTO userOuts=new UserOutDTO();
				PropertyCopyUtil.copyPropertiesExclude(list.get(i),userOuts,"role");
				RoleOutDTO roleOutDTO=new RoleOutDTO();
				if(list.get(i).getRole()==null){
					roleOutDTO.setRoleName(null); 
				}else{
					roleOutDTO.setRoleName(list.get(i).getRole().getRoleName()); 
				}
				userOuts.setRole(roleOutDTO);
				userOutList.add(userOuts);
				userOutDTO.setUserOutList(userOutList);
			}
			userOutDTO.setPage(page); 
			userOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return userOutDTO;
		} catch (ApplicationException ae) { 
			userOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		}
		 catch (Exception e) { 
			userOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return userOutDTO;
	}		
		
	
	
	/**
	 * 添加用户信息
	 * 
	 */
	@RequestMapping(value="/saveUser")
	@ResponseBody
	public UserOutDTO  saveUser(UserInDTO userIn){
		UserOutDTO userOutDTO=new UserOutDTO();
		Userbase user=new Userbase();
		try {  
			user.setBz(userIn.getBz());
			user.setGmtCreate(new Date());
			user.setGmtModified(new Date());
			user.setGroupId(userIn.getGroupId());
			user.setIp("");
			user.setLastLogin(null);
			user.setPassword(new SimpleHash("SHA-1", userIn.getUsername(), userIn.getPassword()).toString());
			user.setRights("");//权限码默认为空
			user.setRoleId(userIn.getRoleId());
			user.setSkin("default");
			user.setStatus(UserStatus.Normal.getType());
			user.setUsername(userIn.getUsername());
			user.setUserType(userIn.getUserType());//用户类型   用户，门店管理员，收银员，代理商，大商户管理员，大商户操作员，掌富管理员，掌富操作员
			user.setName(userIn.getName());
			user.setPhone(userIn.getPhone());
			userService.saveU(user);
			userOutDTO.setErrorCode(Rescode.SUCCESS, "保存成功");
			return userOutDTO;
		} catch (Exception e) { 
			userOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		userOutDTO.setErrorCode("-2", "处理失败");
		return null;
	}
	
	
	
	
	/**
	 * 通过用户id查询用户详情
	 * 
	 */
	@RequestMapping(value="/findByUsername")
	@ResponseBody
	public UserOutDTO  findByUsername(UserInDTO userOut){
		UserOutDTO userOuts=new UserOutDTO();
		Userbase user=new Userbase();
		try {   
			user.setId(userOut.getId());
			Userbase users=userService.findById(user);
			if(users!=null){
				PropertyCopyUtil.copyPropertiesExclude(users,userOuts,"role","page");
				userOuts.setErrorCode(Rescode.SUCCESS, "查询成功");
				return userOuts;
			}else{
				userOuts.setErrorCode("-1", "未查询到记录");
			}
		} catch (Exception e) { 
			userOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		userOuts.setErrorCode("-2", "处理失败");
		return userOuts;
	}
	
	
	
	/**
	 * 通过用户名称查询用户详情
	 * 
	 */
	@RequestMapping(value="/selectUsername")
	@ResponseBody
	public UserOutDTO  selectUsername(UserInDTO userOut){
		UserOutDTO userOuts=new UserOutDTO();
		Userbase user=new Userbase();
		try {   
			user.setId(userOut.getId());
			Userbase users=userService.findByUsername(userOut.getUsername());
			if(users!=null){
				PropertyCopyUtil.copyPropertiesExclude(users,userOuts,"role","page");
				userOuts.setErrorCode(Rescode.SUCCESS, "查询成功");
				return userOuts;
			}else{
				userOuts.setErrorCode("-1", "未查询到记录");
			}
		} catch (Exception e) { 
			userOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		userOuts.setErrorCode("-2", "处理失败");
		return userOuts;
	}
	
	
	/**
	 * 校验用户名  是否存在
	 * 
	 */
	@RequestMapping(value="/has")
	@ResponseBody
	public UserOutDTO  has(UserInDTO userOut){
		UserOutDTO userOuts=new UserOutDTO();
		Userbase user=new Userbase();
		try {   
			user.setUsername(userOut.getUsername());
//			user.setGroupId(userOut.getGroupId());
			int count = userService.ifHas(user);

			if(count==0){
			    //不存在 返回正常状态
				userOuts.setErrorCode(Rescode.SUCCESS, "查询成功");
				return userOuts;
			}else{
				userOuts.setErrorCode("-3", "已存在此数据");
				return userOuts;
			}

//			Userbase users=userService.has(user);
//			if(users==null){
//				//不存在 返回正常状态
//				userOuts.setErrorCode(Rescode.SUCCESS, "查询成功");
//				return userOuts;
//			}else{
//				userOuts.setErrorCode("-3", "已存在此数据");
//				return userOuts;
//			}
		} catch (Exception e) { 
			userOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		userOuts.setErrorCode("-2", "处理失败");
		return userOuts;
	}
	
	
	
	
	/**
	 * 修改用户信息
	 * 
	 */
	@RequestMapping(value="/updateUser")
	@ResponseBody
	public UserOutDTO  updateUser(UserInDTO userOut){
		UserOutDTO userOuts=new UserOutDTO();
		Userbase user=new Userbase();
		try {   
			user.setId(userOut.getId());
			Userbase users=userService.findById(user);
			users.setRoleId(userOut.getRoleId());
			users.setBz(userOut.getBz()); 
			users.setRoleId(userOut.getRoleId());
			if(Tools.notEmpty(userOut.getPassword())){ 
				users.setPassword(new SimpleHash("SHA-1", userOut.getUsername(), userOut.getPassword()).toString());
			}
			users.setName(userOut.getName());
			users.setPhone(userOut.getPhone());
			userService.editUser(users);
			userOuts.setErrorCode(Rescode.SUCCESS, "修改成功");
			return userOuts;
		} catch (Exception e) { 
			userOuts.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		userOuts.setErrorCode("-2", "处理失败");
		return null;
	}



	@RequestMapping(value="/goUpdateGroupOrAgent")
	@ResponseBody
	public UserOutDTO  goUpdateGroupOrAgent(UserInDTO userInDTO){
		UserOutDTO userOutDTO=new UserOutDTO();

		try {
			userOutDTO= userService.queryGroupOrAgentInfo(userInDTO);
			userOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			return userOutDTO;
		} catch (Exception e) {
			userOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		userOutDTO.setErrorCode("-2", "处理失败");
		return null;
	}

	@RequestMapping(value="/updateUserInfo")
	@ResponseBody
	public UserOutDTO  updateUserInfo(UserInDTO userInDTO){
		UserOutDTO userOutDTO=new UserOutDTO();

		try {
			userService.updateUserInfo(userInDTO);
			userOutDTO.setErrorCode(Rescode.SUCCESS, "修改成功");
			return userOutDTO;
		} catch (Exception e) {
			userOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		userOutDTO.setErrorCode("-2", "处理失败");
		return null;
	}




	/**
	 * 删除用户信息
	 * 
	 */
	@RequestMapping(value="/deleteUser")
	@ResponseBody
	public UserOutDTO  deleteUser(UserInDTO userInDTO){
		UserOutDTO userOutDTO=new UserOutDTO();
		Userbase user=new Userbase();
		try {  
			user.setId(userInDTO.getId());
			userService.deleteU(user); 
			userOutDTO.setErrorCode(Rescode.SUCCESS, "删除用户信息成功");
			return userOutDTO;
		} catch (Exception e) { 
			userOutDTO.setErrorCode("-1", "处理出现异常");
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	
	
}
